package com.longe.security;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;

/**
 * 自定义的Permission
 */
@Slf4j
public class MyUrlPermission implements Permission {

    private String url;

    public MyUrlPermission(String url) {
        this.url = url;
    }

    @Override
    public boolean implies(Permission permission) {
        if(!(permission instanceof MyUrlPermission)){
            return false;
        }

        MyUrlPermission urlPermission = (MyUrlPermission)permission;
        PatternMatcher patternMatcher = new AntPathMatcher();

        log.debug("this.url(来自数据库中存放的通配符数据),在 Realm 的授权方法中注入的 => " + this.url);
        log.debug("urlPermission.url(来自浏览器正在访问的链接) => " +  urlPermission.url);
        boolean matches = patternMatcher.matches(this.url,urlPermission.url);
        log.debug("matches => " + matches);
        return matches;
    }
}
